package cn.kginglam.dynamic_excel.strategy.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.kginglam.dynamic_excel.strategy.ImportExcel2DatabaseStrategy;
import cn.kginglam.dynamic_excel.strategy.constant.ImportExcel2DatabaseEnum;
import cn.kginglam.dynamic_excel.strategy.context.ImportExcel2DatabaseContext;
import cn.kginglam.dynamic_excel.strategy.impl.importExcel2Database.StudentInfoImportExcel2DatabaseStrategy;
import cn.kginglam.dynamic_excel.strategy.param.ImportExcel2DatabaseParam;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: Lam
 * @date: 2021-09-29 17:06
 * @description:
 */

public class ImportExcel2DatabaseStrategyFactory {


    /**
     * 获取对应的策略方法
     *
     * @param strategyKey
     * @param param
     * @return
     */
    public static ImportExcel2DatabaseContext getInstantiation(String strategyKey, ImportExcel2DatabaseParam param) {

        ImportExcel2DatabaseStrategy strategy = null;


        // 根据枚举获取到对应的策略
        for (ImportExcel2DatabaseEnum databaseEnum : ImportExcel2DatabaseEnum.values()) {
            if (StrUtil.equals(strategyKey, databaseEnum.getStrategyKey())) {
                strategy = databaseEnum.getImportExcel2DatabaseStrategy();
            }
        }

        // 如果没有对应的策略，抛出异常
        if (ObjectUtil.isNull(strategy)) {
            try {
                throw new Exception("没有对应的策略类");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 返回上下文
        return new ImportExcel2DatabaseContext(strategy, param);
    }

}
